#include <iostream>
#include <iomanip>
#include <fstream>
using std::ifstream;
using std::ofstream;
#include "fisier.h"
#include <cstring>
using std::strcmp;
using std::strcpy;
using std::strcat;
int i=0;
using std::setw;
using std::cout;
using std::cin;
String::String(const char*s):length((s!=0)?strlen(s):0)
{
	setString(s);
}
String::~String()
{
	delete [] sPtr;
}
bool String::operator==(const String &right)const
{
	return strcmp((*this).sPtr,right.sPtr)==0;
}
bool String::operator<(const String &right)const
{
	return strcmp((*this).sPtr,right.sPtr)<0;
}

int String::getLength()const
{
	return (*this).length;
}
void String::setString(const char *str2)
{
	(*this).sPtr=new char[(*this).length+1];
	if(str2!=0)
		strcpy((*this).sPtr,str2);
	else
		(*this).sPtr[0]='\0';
}
std::ostream &operator<<(std::ostream &out,const String &obj)
{
	out<<obj.sPtr;
	return out;
}
std::istream &operator >>(std::istream &inp,String &obj)
{
	char temp[100];
	inp>>setw(100)>>temp;
	obj=temp;
	return inp;
}
std::ifstream &operator >>(std::ifstream &inp,String &obj)
{
	char temp[100];
	inp>>setw(100)>>temp;
	obj=temp;
	return inp;
}
bool String::operator>(const String& right)const{
		return right<*this;
}
bool String::operator!=(const String& right)const{
		return !(*this==right);
}
bool String::operator <=(const String& right)const{
		return !(right<*this);
}
bool String::operator >=(const String& right)const{
		return !(*this<right);
}
const String &String::operator =(const String &right)
{
	if(&right!=this)
	{
		delete [] sPtr;
		(*this).length=right.length;
		setString(right.sPtr);
	}
	else
		std::cout<<"Nu poti asigna acelasi obiect\n";
	return *this;
}
const String &String::operator +=(const String &right)
{
	size_t newLength=(*this).length+right.length;
	char *temp=new char[newLength+1];
	strcpy(temp,(*this).sPtr);
	strcpy(temp+(*this).length,right.sPtr);
	delete [] sPtr;
	(*this).sPtr=temp;
	(*this).length=newLength;
	return *this;
}

void Fisier::citire(){
   ifstream fin("cuvinte.inp");
    ofstream fout("cuvinte.out");
    String linie;
    while(!fin.eof())
    {
        fin>>linie;
        v[++i]=linie;
    }
}
void Fisier::afisare(){
		for(int it=1;it<i;it++)
			cout<<v[it];
}
void Fisier::sortare(){
    int ii, jj;
		 String aux;
    for (ii = 1; ii < i; ii++)
     {
        jj = ii;
        while (jj > 0 && v[jj - 1] > v[jj])
        {
            aux = v[jj];
            v[jj] = v[jj - 1];
            v[jj - 1] = aux;
            jj--;
        }
     }

}
void Fisier::scriere(){
	ofstream fout("cuvinte.out");
	for(int it=1;it<i;it++)
				{int x=0;
					for(int itt=it+1;itt<i;itt++)
					{
							if(v[it]==v[itt])
							{
										x++;
							}
					}
				fout<<"\n"<<v[it]<<" se repeta de "<<x<< "ori\n";
				}

}
